package br.com.portalecbahia.customize;

import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.portalecbahia.R;
import br.com.portalecbahia.model.Action;

public class ActionListAdapter extends BaseAdapter {
    
	private Context context;

    private List<Action> listAction;

	public Action action;
    public ActionListAdapter (Context context, List<Action> listAction){
        this.context = context;
        this.listAction = listAction;
    }
    
	public int getCount() {
		// TODO Auto-generated method stub
		return  listAction.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listAction.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
        // Get actual object
		 action= listAction.get(position);
		 // on View
        LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_list_actions , null);
        
      
        	
        TextView minuteTX = (TextView)view.findViewById(R.id.minuteTX);
//        if(action.getMinutes()!=null){
//        	Integer minutes = new Integer(action.getMinutes());
//        	if(minutes<=45){
//        		minuteTX.setText(action.getMinutes()+"'");
//        	}else{
//        		minutes=minutes-45;
//        		minuteTX.setText(minutes+"''");
//        	}
//        }
        
        TextView teamTX = (TextView)view.findViewById(R.id.teamTX);
        if(action.getTeam()!=null && !"null".equals(action.getTeam())){
        	teamTX.setText(action.getTeam());
        }
        
        if(action.getMinutes()!=null && action.getType().getId()<=5){
        	minuteTX.setText(action.getMinutes());
        }else if(action.getMinutes()!=null && action.getType().getId()>=5){
        	teamTX.setText(action.getMinutes());
        }
        
        TextView descriptionTX = (TextView)view.findViewById(R.id.descriptionTX);
        if(action.getDescription()!=null ){
        	descriptionTX.setText(action.getDescription());
        }
        
        
        ImageView actionIMG = (ImageView)view.findViewById(R.id.actionIMG);
        if(action.getType()!=null){
        	if(action.getType().getId()==1){
        		actionIMG.setImageResource(R.drawable.game_yellowcard);
        	}else if(action.getType().getId()==2){
        		actionIMG.setImageResource(R.drawable.game_redcard);
        	}else if(action.getType().getId()==3){
        		actionIMG.setImageResource(R.drawable.game_ac);
        	}else if(action.getType().getId()==4){
        		actionIMG.setImageResource(R.drawable.game_gol);
        		descriptionTX.setTextColor(Color.RED);
        		SpannableString spanString = new SpannableString(descriptionTX.getText().toString());
        		spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
        		descriptionTX.setText(spanString);
        		minuteTX.setTextColor(Color.RED);
        		teamTX.setTextColor(Color.RED);
        	
        	}
        }
   	 Button shareButton = (Button) view.findViewById(R.id.shareBTN);
     if(action.getMinutes()!=null)
    	shareButton.setVisibility(View.VISIBLE);
   	 	

       
        return view;
	}

	
	 
	
	

}
